package com.songtech.auth.role.controller;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.github.pagehelper.PageInfo;
import com.songtech.auth.role.model.Role;
import com.songtech.auth.role.service.RoleService;
import com.songtech.auth.user.model.User;
import com.songtech.base.common.BaseConstant;
import com.songtech.base.common.JsonResult;

@Controller
@RequestMapping("/auth/role")
public class AuthRoleController {
	
	@Autowired
	private RoleService roleService;
	
	/**
	 * 查询所有角色
	 * @return
	 */
	@RequestMapping("/getRoles")
	@ResponseBody
	public Object getAllRole(Role role,String page, String limit){
		int pageNum = 0;
		int pageSize = 10;
		if(!StringUtils.isEmpty(page)){
			pageNum = Integer.valueOf(page);
		}
		if(!StringUtils.isEmpty(limit)){
			pageSize = Integer.valueOf(limit);
		}
		PageInfo<Role> roles = roleService.queryRoleAll(role,pageNum,pageSize);
		JsonResult result = new JsonResult(roles);
		result.status = BaseConstant.SUCCESS_CODE;
		result.msg = BaseConstant.SUCCESS;
		return result;
	}
	
	/**
	 * 新增角色
	 * @param role
	 */
	@RequestMapping("/add")
	@ResponseBody
	public Object addRole(Role role){
		JsonResult result = new JsonResult();
		int index = roleService.addRole(role);
		if(index==-1){
			result.status = BaseConstant.FAIL_CODE;
			result.msg = "已经存在相同编码的角色";
		}else if(index==0){
			result.status = BaseConstant.FAIL_CODE;
			result.msg = "角色名称或者角色编码为空 ";
		}else{
			result.status = BaseConstant.SUCCESS_CODE;
		}
		return result;
	}
	/**
	 * 修改
	 * @param role
	 * @return
	 */
	@RequestMapping("/update")
	@ResponseBody
	public Object updateRole(Role role){
		JsonResult result = new JsonResult();
		int index = roleService.updateRole(role);
		if(index==-2){
			result.status = BaseConstant.FAIL_CODE;
			result.msg = "已经存在相同编码的角色";
		}else if(index==-1){
			result.status = BaseConstant.FAIL_CODE;
			result.msg = "未找到当前角色信息 ";
		}else if(index==0){
			result.status = BaseConstant.FAIL_CODE;
			result.msg = "角色标识ID为空 ";
		}else{
			result.status = BaseConstant.SUCCESS_CODE;
		}
		return result;
	}
	/**
	 * 分页查询角色对应的所有用户
	 * @param roleId
	 * @return
	 */
	@RequestMapping("/getRoleUsers")
	@ResponseBody
	public Object getRoleUsers(String roleId,String page, String limit){
		int pageNum = 0;
		int pageSize = 10;
		if(!StringUtils.isEmpty(page)){
			pageNum = Integer.valueOf(page);
		}
		if(!StringUtils.isEmpty(limit)){
			pageSize = Integer.valueOf(limit);
		}
		PageInfo<User> users = roleService.getRoleUsers(roleId,pageNum,pageSize);
		return users;
	}
	/**
	 * 查询角色拥有的用户
	 * @param roleId
	 * @return
	 */
	@RequestMapping("/getRoleAllUser")
	@ResponseBody
	public Object getRoleAllUser(String roleId){
		List<User> users = roleService.getRoleAllUser(roleId);
		JsonResult result = new JsonResult(users);
		result.status = BaseConstant.SUCCESS_CODE;
		result.msg = BaseConstant.SUCCESS;
		return result;
	}
	/**
	 * 查看角色拥有的资源信息
	 * @param roleId
	 * @return
	 */
	@RequestMapping("/getRoleResource")
	@ResponseBody
	public Object getRoleResources(String roleId){
		return roleService.getRoleRes(roleId);
	}
	/**
	 * 新增角色资源权限
	 * @param roleId
	 * @param resIdList
	 * @return
	 */
	@RequestMapping("/insertRoleRes")
	@ResponseBody
	public Object insertRoleRes(String roleId,String resIds){
		List<String> resIdList = new ArrayList<>();
		if(!StringUtils.isEmpty(resIds)){
			String ids[] = resIds.split(",");
			resIdList = Arrays.asList(ids);
		}
		roleService.insertRoleRes(roleId,resIdList);
		JsonResult result = new JsonResult();
		result.status = BaseConstant.SUCCESS_CODE;
		result.msg = BaseConstant.SUCCESS;
		return result;
	}
	/**
	 * 新增角色用户关系
	 * @param roleId
	 * @param resIdList
	 * @return
	 */
	@RequestMapping("/insertRoleUser")
	@ResponseBody
	public Object insertRoleUsers(String roleId,String userIds){
		List<String> userIdsList = new ArrayList<>();
		if(!StringUtils.isEmpty(userIds)){
			String ids[] = userIds.split(",");
			userIdsList = Arrays.asList(ids);
		}
		roleService.insertRoleUsers(roleId,userIdsList);
		JsonResult result = new JsonResult();
		result.status = BaseConstant.SUCCESS_CODE;
		result.msg = BaseConstant.SUCCESS;
		return result;
	}
	
	/**
	 * 启用角色
	 * @param roleId
	 * @return
	 */
	@RequestMapping("/open")
	@ResponseBody
	public Object eabledRole(String roleId){
		return roleService.eabledRole(roleId);
	}
	
	/**
	 * 停用角色
	 * @param roleId
	 * @return
	 */
	@RequestMapping("/stop")
	@ResponseBody
	public Object disabledRole(String roleId){
		return roleService.disabledRole(roleId);
	}
	
	/**
	 * 逻辑删除角色
	 * @param roleId
	 * @return
	 */
	@RequestMapping("/delete")
	@ResponseBody
	public Object deleteRole(String roleId){
		return roleService.deleteRole(roleId);
	}
}
